package com.deliinsns.app.security;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.deliinsns.app.employer.EmployerDao;
import com.deliinsns.app.employer.bean.Employer;
import com.deliinsns.app.security.bean.WebUser;
import com.deliinsns.app.student.StudentDao;
import com.deliinsns.app.student.bean.Student;
import com.deliinsns.sys.BasicController;
import com.deliinsns.sys.Messenger;
import com.deliinsns.sys.MsgType;
import com.deliinsns.sys.util.PropertyUtils;
import com.deliinsns.sys.util.SystemConstant;

@Controller
public class SecurityController extends BasicController {
	
	 @Autowired  
     private  StudentDao studentDao;  
	 @Autowired  
     private  EmployerDao employerDao;  
	//学生和雇主登陆
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	 public String login(WebUser user,HttpServletRequest request,HttpServletResponse response) throws Exception {
		String nextPage= "login";
		String userType= request.getParameter("userType");
		request.setAttribute("userType", userType);
		WebUser curUser= null;
		if(checkParam(userType)){
			if("student".equals(userType)){
				curUser = studentDao.getByUsername(user.getUsername());
			}else if("employer".equals(userType)){
				curUser = employerDao.getByUsername(user.getUsername());
			}
		}
		if(curUser!=null&&curUser.getPassword().equals(user.getPassword())){	//用户存在且密码正确
			request.getSession().setAttribute("user", curUser);
			nextPage= userType+"/index";
		}else{
			Properties ppt= getCodeDesc(request);
			request.setAttribute(userType+"Tip", ppt.getProperty(SystemConstant.LOGIN_FAILD));
		}
		//request.getSession().setAttribute("tip", "测试消息！");
		
		return nextPage;
	 }
	@RequestMapping(value = "username/validate/{username}", method = RequestMethod.GET)
	 public void userNameValidate(@PathVariable String username ,HttpServletRequest request,HttpServletResponse response) throws Exception {
		Messenger msg= new Messenger();
		if(checkParam(username)){				//如果所有参数都正确传入才进行相应处理
			Student student= studentDao.getByUsername(username);
			Employer employer= employerDao.getByUsername(username);
			  if(employer==null&&student==null){//表示该用户名还没有人用，即可注册。
				  msg.setState(200);
				  msg.setMsg(SystemConstant.GET_SUCCESS);
			  }else{
				  msg.setState(202);
				  msg.setMsg(SystemConstant.GET_FAILD);
			  }
		}else{
			msg.setMsg(SystemConstant.PARAM_ERROR);
		}
		out(request,response,msg);
	 }

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	 public String test(HttpServletRequest request,HttpServletResponse response)throws Exception {
		Messenger msg= new Messenger();
		msg.setState(200);
		msg.setMsg("yes123");
		//out(response,MsgType.msg);
		//String propertyFilePath= "/WEB-INF/config/constant.properties";
		String propertyFilePath=request.getSession().getServletContext().getRealPath("WEB-INF/config/constant.properties");
		Properties ppts= PropertyUtils.getProperties(propertyFilePath);
		System.out.println(ppts.get("name"));
		System.out.println(ppts.get("age"));
		request.setAttribute("aaa", "twq");
		return "welcome";
	 }
	
	

}
